﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
/*
namespace ExamServer
{
    public class Clients
    {
        /// <summary>
        /// 客户端连接的集合
        /// </summary>
        private Client[] client = new Client[Properties.Settings.Default.ConnectionNumber];

        /// <summary>
        /// 连接数
        /// </summary>
        private int count = 0;
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        /// <summary>
        /// 索引与真实连接号的映射
        /// </summary>
        private int[] mapping = new int[Properties.Settings.Default.ConnectionNumber];

        /// <summary>
        /// 用来增加新连接的真实连接号
        /// </summary>
        private int trueNumber = 0;

        /// <summary>
        /// 访问某个Client的索引
        /// </summary>
        /// <param name="i">索引号</param>
        /// <returns>返回对应的Client，超出范围则返回null</returns>
        public Client this[int i]
        {
            get 
            { 
                if (i > count) return null; 
                else return client[mapping[i]];
            }
        }

        /// <summary>
        /// 往连接集合中增加新的连接
        /// </summary>
        /// <param name="c">Client</param>
        /// <returns>成功则返回其索引号</returns>
        public int Add(Client c)
        {
            for (int i = 0; i < count; i++)
            {
                if (trueNumber == mapping[i])
                {
                    if (trueNumber == Properties.Settings.Default.ConnectionNumber-1) trueNumber = 0;
                    else trueNumber++;
                    i = -1;
                    continue;
                }
            }
            client[trueNumber] = c;
            mapping[count] = trueNumber;
            count++;
            if (trueNumber == Properties.Settings.Default.ConnectionNumber-1)
            {
                trueNumber = 0;
            }
            else
            {
                trueNumber++;
            }
            return count - 1;
        }

        /// <summary>
        /// 断开连接集合中指定的连接，终止其线程
        /// </summary>
        /// <param name="i">索引号</param>
        public void RemoveAt(int i)
        {
            client[mapping[i]].WorkSock.Shutdown(SocketShutdown.Both);
            client[mapping[i]].WorkSock.Close();
        }

        public void RemoveByUserName(string userName)
        {
            for (int i = 0; i < count; i++ )
            {
                if (client[mapping[i]].UserName == userName)
                {
                    client[mapping[i]] = null;
                    for (int j = i; j < count - 1; j++)
                    {
                        mapping[j] = mapping[j + 1];
                    }
                    count--;
                    return;
                }
            }
        }

        public void OutLineByUserName(string userName)
        {
            for (int i = 0; i < count; i++)
            {
                if (client[mapping[i]].UserName == userName)
                {
                    try
                    {
                        string str = "与服务器的连接中断！";
                        client[mapping[i]].Send("1006", str);
                        for (int j = i; j < count - 1; j++)
                        {
                            mapping[j] = mapping[j + 1];
                        }
                        count--;
                        Thread.Sleep(500);
                        client[mapping[i]].WorkSock.Close();
                    }
                    catch(Exception)
                    {
 
                    }
                    return;
                }
            }
        }
    }
}
*/